package com.jiamao.goplay.ui.login;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.config.Config;
import com.jiamao.goplay.databinding.LoginPhoneLayoutBinding;
import com.jiamao.goplay.databinding.LoginWechatLayoutBinding;
import com.jiamao.goplay.ui.login.vm.LoginViewModel;
import com.jiamao.goplay.ui.login.vm.LoginWeChatModel;
import com.jiamao.goplay.ui.web.WebFragment;
import com.tamsiree.rxkit.view.RxToast;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import androidx.activity.OnBackPressedDispatcher;
import androidx.activity.OnBackPressedDispatcherOwner;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import me.goldze.mvvmhabit.utils.KLog;


/**
 * 一个MVVM模式的登陆界面
 */
public class LoginWeChatFragment extends BaseMVVMFragment<LoginWechatLayoutBinding, LoginWeChatModel> {

    //ActivityLoginBinding类是databinding框架自定生成的,对应activity_login.xml

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.login_wechat_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public LoginWeChatModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(LoginWeChatModel.class);
    }


    @Override
    public void initViewObservable() {





        SpannableString spannableString = new SpannableString("点击同意玩多多的用户协议与隐私条款");


        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                Bundle bd = new Bundle();
                bd.putString("url", Config.WEB_YHXY);
                bd.putString("title","用户协议");
                startContainerActivity(WebFragment.class.getCanonicalName(),bd);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
             //   ds.setColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));//设置颜色
                ds.setUnderlineText(true);//去掉下划线
            }
        };
        spannableString.setSpan(clickableSpan, 8, 12, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        ClickableSpan clickableSpan2 = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                Bundle bd = new Bundle();
                bd.putString("url", Config.WEB_YSZC);
                bd.putString("title","隐私条款");
                startContainerActivity(WebFragment.class.getCanonicalName(),bd);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
             //   ds.setColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));//设置颜色
                ds.setUnderlineText(true);//去掉下划线
            }

        };
        spannableString.setSpan(clickableSpan2, 13, 17, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);


      //  ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.colorPrimary));
     //   spannableString.setSpan(colorSpan, 9, 13, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

       // ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.colorPrimary));
      // spannableString.setSpan(colorSpan2, 16, 20, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        binding.tvxy.setText(spannableString);
        binding.tvxy.setMovementMethod(LinkMovementMethod.getInstance());
        binding.tvxy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                binding.ckXy.setChecked(!binding.ckXy.isChecked());
            }
        });
        viewModel.uc.wechatLogin.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
//               UMShareAPI.get(getContext()).getPlatformInfo();

                if(!binding.ckXy.isChecked()){
                    RxToast.showToastShort("请先同意用户协议与隐私条款");
                    return;
                }
                if (aBoolean){
                    startContainerActivity(LoginPhoneFragment.class.getCanonicalName());
                }else {
                    UMShareAPI.get(getContext()).getPlatformInfo(getActivity(), SHARE_MEDIA.WEIXIN, new UMAuthListener() {
                        @Override
                        public void onStart(SHARE_MEDIA share_media) {
                            KLog.d("start "+share_media.getName());
                            showDialog("请求中...");

                        }

                        @Override
                        public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                            KLog.d("onComplete "+share_media.getName()+map.toString());
                            dismissDialog();
                            for(Object obj : map.keySet()){
                                Object value = map.get(obj );

                                KLog.d("key: "+obj+" value:"+value);
                            }
                            String unionid = map.get("unionid");
                            String openid = map.get("openid");
                            viewModel.loginWeChat(unionid,openid);
                        }

                        @Override
                        public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                            KLog.d("onError "+i);
                            throwable.printStackTrace();
                            dismissDialog();
                            RxToast.showToastShort("微信登陆异常");
                        }

                        @Override
                        public void onCancel(SHARE_MEDIA share_media, int i) {
                            KLog.d("onCancel "+share_media.getName());
                            dismissDialog();
                            RxToast.showToastShort("微信登陆取消");
                        }
                    });

                }

            }
        });

    }

    @Override
    public void initData() {
        super.initData();


        ImmersionBar.with(this).keyboardEnable(false).statusBarDarkFont(true).init();



    }


}
